//
//	MiscDistInspector.m -- Inspector object for MiscDistributor.
//
//		Written by Don Yacktman.  Copyright 1994 by Don Yacktman.
//				Version 1.0  All rights reserved.
//
//		This notice may not be removed from this source code.
//
//	This object is included in the MiscKit by permission from the author
//	and its use is governed by the MiscKit license, found in the file
//	"LICENSE.rtf" in the MiscKit distribution.  Please refer to that file
//	for a list of all applicable permissions and restrictions.
//	

#import "MiscDistInspector.h"
#import "MiscDistributorPrivate.h"
#import "_MiscDistributorConnection.h"

@implementation MiscDistributor(IBSupport)

- (const char *)getInspectorClassName
{
	return "MiscDistributorInspector";
}

- (NXImage *)getIBImage
{
    id	idBundle;
    id	idImage;
	char buf[MAXPATHLEN + 1];
	
	idBundle = [NXBundle bundleForClass:[MiscDistributorInspector class]];
    [idBundle getPath:buf forResource:"MiscDistributor" ofType:"tiff"];
    idImage = [[NXImage alloc] initFromFile:buf];
	
	return idImage;
}

@end

@implementation MiscDistributorInspector

- init
{	
    char buf[MAXPATHLEN + 1];
    id bundle;
	[super init];
    bundle = [NXBundle bundleForClass:[MiscDistributor class]];
    [bundle getPath:buf forResource:"MiscDistributorInspector" ofType:"nib"];
    [NXApp loadNibFile:buf owner:self withNames:NO fromZone:[self zone]];
	return self;
}

- (BOOL)wantsButtons { return NO; }

- revert:sender
{
	connections = [object _connectionList];
	[self setUpMatrix];
	[self selectRow:0 notify:YES];
	return [super revert:sender];
}

- ok:sender
{
	return [super ok:sender];
}

- setUpMatrix
{
	int i;
	// populate matrix with connections' names
	[connectionMatrix renewRows:[connections count] cols:1];
	for (i=0; i<[connections count]; i++) {
		[[connectionMatrix cellAt:i :0]
				setTitle:[[connections objectAt:i] connectionName]];
	}
	[connectionMatrix sizeToCells];
	[connectionMatrix display];
	return self;
}

- editName
{
	[window makeFirstResponder:connectionNameText];
	[connectionNameText selectText:self];
	return self;
}

- selectRow:(int)i notify:(BOOL)flag
{
	[connectionMatrix selectCellAt:i :0];
	[self selectionChanged:connectionMatrix];
	[connectionMatrix display];
	[self editName];
	return self;
}

- selectionChanged:sender
{
	// change values of name/dir widgets to reflect selected connection
	selObject = [connections objectAt:[connectionMatrix selectedRow]];
	[connectionNameText setStringValue:[selObject connectionName]];
	if ([selObject direction] == MiscIn) {
		[connectionDirMatrix selectCellAt:0 :0];
	} else if ([selObject direction] == MiscOut) {
		[connectionDirMatrix selectCellAt:0 :1];
	} else if ([selObject direction] == MiscInout) {
		[connectionDirMatrix selectCellAt:0 :2];
	}
	[self editName];
	return self;
}

- addConnection:sender
{
	id newConnection = [[_MiscDistributorConnection alloc] init];
	// add a connection to the list and matrix, and select it in the matrix
	[connections addObject:newConnection];
	[newConnection setConnectionName:"UNTITLED"];
	[newConnection setDirection:MiscInout];
	[self setUpMatrix];
	[self selectRow:([connections count] - 1) notify:YES];
	return self;
}

- changeConnection:sender
{
	int theRow = [connectionMatrix selectedRow];
	// take values of name/dir widgets and change selected connection
	switch ([connectionDirMatrix selectedCol]) {
		case 0 : [selObject setDirection:MiscIn]; break;
		case 1 : [selObject setDirection:MiscOut]; break;
		case 2 : [selObject setDirection:MiscInout]; break;
		default: [selObject setDirection:MiscIn]; break;
	}
	[selObject setConnectionName:[connectionNameText stringValue]];
	[self setUpMatrix];
	[self selectRow:theRow notify:NO];
	return self;
}

@end
